/**
 * 需要去天地图官网申请Key，然后把下面第121行的tk的值换成自己申请的浏览器端的Key。
   原理：使用dojo/_base/declare模块，继承API中的TiledMapServiceLayer类，自定义天地图切片加载。
 */
import { loadModules } from "esri-loader";
const options = {
  version: "3.34",
  css: true,
  url: "https://js.arcgis.com/3.34/init.js",
};
export default async function () {
  const [declare, TileInfo, SpatialReference, Extent, TiledMapServiceLayer] =
    await loadModules(
      [
        "dojo/_base/declare",
        "esri/layers/TileInfo",
        "esri/SpatialReference",
        "esri/geometry/Extent",
        "esri/layers/TiledMapServiceLayer",
      ],
      options
    );
  return declare("TDTLayer", TiledMapServiceLayer, {
    constructor: function (d_1) {
      this.mapType = d_1.type;
      this.spatialReference = new SpatialReference({ wkid: 4326 });
      this.initialExtent = this.fullExtent = new Extent(
        -180,
        -90,
        180,
        90,
        this.spatialReference
      );
      this.tileInfo = new TileInfo({
        rows: 256,
        cols: 256,
        compressionQuality: 0,
        origin: {
          x: -180,
          y: 90,
        },
        spatialReference: {
          wkid: 4326,
        },
        lods: [
            { level: 2, resolution: 0.3515625, scale: 147748796.52937502 },
            { level: 3, resolution: 0.17578125, scale: 73874398.26468751 },
            { level: 4, resolution: 0.087890625, scale: 36937199.132343754 },
            { level: 5, resolution: 0.0439453125, scale: 18468599.566171877 },
            { level: 6, resolution: 0.02197265625, scale: 9234299.783085939, },
            { level: 7, resolution: 0.010986328125, scale: 4617149.891542969, },
            { level: 8, resolution: 0.0054931640625, scale: 2308574.9457714846, },
            { level: 9, resolution: 0.00274658203125, scale: 1154287.4728857423, },
            { level: 10, resolution: 0.001373291015625, scale: 577143.7364428712, },
            { level: 11, resolution: 0.0006866455078125, scale: 288571.8682214356, },
            { level: 12, resolution: 0.00034332275390625, scale: 144285.9341107178, },
            { level: 13, resolution: 0.000171661376953125, scale: 72142.9670553589, },
            { level: 14, resolution: 0.0000858306884765625, scale: 36071.48352767945, },
            { level: 15, resolution: 0.00004291534423828125, scale: 18035.741763839724, },
            { level: 16, resolution: 0.000021457672119140625, scale: 9017.870881919862, },
            { level: 17, resolution: 0.000010728836059570312, scale: 4508.935440959931, },
            { level: 18, resolution: 0.000005364418029785156, scale: 2254.4677204799655, },
        ],
      });
      this.loaded = true;
      this.onLoad(this);
    },
    getTileUrl: function (level_1, row, col) {
      let tk = "603db27bde1f91a7eb09bf3e4f8da9bb"; //天地图官网申请Key
      let DDO = {
        //数据字典
        vec: "vec", //矢量底图
        img: "img", //影像底图
        cva: "cva", //矢量注记
      };
      // console.log(DDO[this.mapType])
      return (
        "http://t" +
        (col % 8) +
        ".tianditu.gov.cn/" +
        DDO[this.mapType] +
        "_c/wmts?" +
        "tk=" +
        tk +
        "&service=wmts&request=GetTile&version=1.0.0&LAYER=" +
        DDO[this.mapType] +
        "&tileMatrixSet=c&TileMatrix=" +
        level_1 +
        "&TileRow=" +
        row +
        "&TileCol=" +
        col +
        "&style=default&format=tiles"
      );
    },
  });
}
